package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.DelReq;
import com.cloud.lan.api.dto.req.LanTypeReq;
import com.cloud.lan.dao.LanTypeMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.exceptions.BaseException;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanTypeDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanTypeService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 侧边栏类目表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-01
 */
@Slf4j
@Service
public class LanTypeServiceImpl extends ServiceImpl<LanTypeMapper, LanTypeDO> implements ILanTypeService {

    @Resource
    LanTypeMapper lanTypeMapper;

    @Override
    public BaseResponse manageList(LanTypeReq req) {
        log.info("#查询 侧边栏类目 列表 {}", req);
        LambdaQueryWrapper<LanTypeDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanTypeDO::getIsDeleted, false).orderByDesc(LanTypeDO::getCreateTime);
        if (StrUtil.isNotEmpty(req.getName())) {
            qw.like(LanTypeDO::getName, req.getName());
        }
        Page<LanTypeDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanTypeDO> pageList = lanTypeMapper.selectPage(page, qw);
        pageList.getRecords().forEach(p -> {
            if (StrUtil.isNotEmpty(p.getParentId())) {
                LambdaQueryWrapper<LanTypeDO> ltd = new LambdaQueryWrapper<>();
                List<Long> prId = JSONUtil.toList(p.getParentId(), Long.class);
                prId.remove(p.getId());
                ltd.in(LanTypeDO::getId, prId);
                p.setChildren(lanTypeMapper.selectList(ltd));
            }
        });
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanTypeReq req) {
        log.info("#新增 侧边栏类目表 {}", req);
        VocUtil.checkNullAttribute(new String[]{req.getName()},
                new String[]{"name"}, LanError.ILLEGAL_PARAMS.getCode());
        LanTypeDO lanTypeDO = new LanTypeDO();
        BeanUtil.copyProperties(req, lanTypeDO);
        LambdaQueryWrapper<LanTypeDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanTypeDO::getName, lanTypeDO.getName());
        luwQw.eq(LanTypeDO::getIsDeleted, false);
        if (lanTypeMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_040002.getMsg());
            throw new BaseException(LanError.LAN_040002);
        }
        lanTypeDO.setProducts(JSONUtil.toJsonStr(req.getProducts()));
        lanTypeMapper.insert(lanTypeDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(LanTypeReq req) {
        log.info("#更新 侧边栏类目表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())},
                new String[]{"id"}, LanError.ILLEGAL_PARAMS.getCode());
        LanTypeDO lanTypeDO = lanTypeMapper.selectById(req.getId());
        if (ObjectUtil.isEmpty(lanTypeDO)) {
            log.error("{}", LanError.LAN_010008.getMsg());
            throw new BaseException(LanError.LAN_010008);
        }
        BeanUtil.copyProperties(req, lanTypeDO);
        lanTypeDO.setUpdateTime(DateUtil.date());
        lanTypeMapper.updateById(lanTypeDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(DelReq req) {
        log.info("#删除 侧边栏类目表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.LAN_040001.getCode());
        LambdaUpdateWrapper<LanTypeDO> luw = new LambdaUpdateWrapper<>();
        luw.set(LanTypeDO::getIsDeleted, ONE.getKey())
                .set(LanTypeDO::getUpdateTime, DateUtil.date())
                .eq(LanTypeDO::getId, req.getId());
        lanTypeMapper.update(new LanTypeDO(), luw);
        return BaseResponse.success();
    }
}
